package com.swz.dcrm.ui.message;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.adpter.MultiItemTypeAdapter;
import com.swz.dcrm.adpter.TodayPlanAdapter;
import com.swz.dcrm.adpter.WorkRemindAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.WorkRemind;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.home.TodayPlanFragmentArgs;
import com.swz.dcrm.ui.viewmodel.MainViewModel;
import com.swz.dcrm.ui.viewmodel.TodayPlanViewModel;
import com.swz.dcrm.util.Constant;
import com.swz.dcrm.util.CustomDecoration;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;
import com.xh.baselibrary.callback.OnClickListener;

import java.util.List;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class WorkRemindFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.rv)
    RecyclerView rv;

    @Inject
    MainViewModel mainViewModel;
    @Inject
    TodayPlanViewModel todayPlanViewModel;



    private WorkRemindAdapter workRemindAdapter;


    public static WorkRemindFragment newInstance() {

        return new WorkRemindFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
        todayPlanViewModel.getWorkRemind().observe(this, observer);
    }
    Observer observer=new Observer<BaseResponse<List<WorkRemind>>>() {
        @Override
        public void onChanged(BaseResponse<List<WorkRemind>> listBaseResponse) {
            if(listBaseResponse.isSuccess()){
                if(workRemindAdapter==null){
                    workRemindAdapter=new WorkRemindAdapter(getContext(),listBaseResponse.getData());
                    StickyRecyclerHeadersDecoration stickyRecyclerHeadersDecoration=new StickyRecyclerHeadersDecoration(workRemindAdapter);
                    rv.addItemDecoration(stickyRecyclerHeadersDecoration);
                    workRemindAdapter.setOnClickListener(onClickListener);
                    rv.setAdapter(workRemindAdapter);
                }else {
                    workRemindAdapter.refresh(1,listBaseResponse.getData());
                }
            }
        }
    };

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_work_remind;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.work_remind_title));
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        rv.addItemDecoration(new CustomDecoration(getContext(),0,10,15,15));

        return true;
    }

    OnClickListener<WorkRemind> onClickListener= workRemind -> {

        TodayPlanFragmentArgs.Builder builder=new TodayPlanFragmentArgs.Builder();


        switch (workRemind.getCode()){
            case 1:
                builder.setType(Constant.TODAY_ARRIVE);
                break;
            case 2:
                builder.setType(Constant.TODAY_CONTACT);
                break;
            case 3:
                go(mainViewModel,R.id.action_workRemindFragment_to_nearlyDeliveryFragment,null);
                return;
            case 4:
                builder.setType(Constant.TODAY_BOOKING);
                break;
        }
        go(mainViewModel,R.id.action_workRemindFragment_to_todayContactFragment,builder.build().toBundle());
    };

    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
}
